package com.KKB.service.impl;

import com.KKB.bean.Menu;
import com.KKB.bean.Role;
import com.KKB.bean.User;
import com.KKB.dao.RoleDao;
import com.KKB.dao.UserDao;
import com.KKB.dao.impl.RoleDaoImpl;
import com.KKB.dao.impl.UserDaoImpl;
import com.KKB.service.UserService;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDaoImpl();
    private RoleDao roleDao=new RoleDaoImpl();
    @Override
    public User login(String username, String password) {
        User user = userDao.login(username, password);
        if (user==null){
            return null;
        }
        Integer roleId = user.getRoleId();
        Role role = roleDao.findbyid(roleId);

        List<Menu> menuList = role.getMenuList();
        List<Menu> newMenuList=new ArrayList<>();
        for (Menu menu:menuList) {
            if (menu.getUpmenuID()==0){
                List<Menu> secondList = new ArrayList<>();
                for (Menu second:menuList) {
                    if (second.getUpmenuID()==menu.getMenuId()){
                        secondList.add(second);
                    }
                }
                menu.setSecondMenuList(secondList);
                newMenuList.add(menu);
            }
        }
        role.setMenuList(newMenuList);
        user.setRole(role);

        return user;
    }

    @Override
    public List<User> getUserList(int pageIndex, int pageSize) {
        return userDao.getUserList(pageIndex,pageSize);
    }

    @Override
    public int total() {
        return userDao.total();
    }

    @Override
    public int addUsers(User user) {
        return userDao.addUsers(user);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public User findById(int uid) {
        return userDao.findById(uid);
    }
    @Override
    public int deleteById(int uid) {
        return userDao.deleteById(uid);
    }
}
